﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using Core;
using Core.DataProvider;

namespace Service.Agent
{
    class SqlCheck
    {
        internal static void PerformCheck(SqlInstance sqlInstance, Entity parent, Entity topLevel)
        {
            string connectionString = "Data Source=" + sqlInstance.NetworkName + ";Initial Catalog=master;Integrated Security=SSPI;app=SqlMonitoring.Agent";
            using (SqlConnector sc = new SqlConnector(connectionString))
            {
                EntityType instance = (from i in MainService.EntityTypeList where i.Uid == "FF4740CE-DA0C-4FC1-98D3-59854A5EA3DA" select i).FirstOrDefault();                
                if (instance != null)
                    ProcessSqlCheck(connectionString, instance, parent, topLevel);
            }
        }

        static void ProcessSqlCheck(string connectionString, EntityType currentType, Entity parent, Entity topLevel)
        {
            using (SqlConnector sc = new SqlConnector(connectionString))
            {
                foreach (DataRow r in sc.ExecuteSqlText(currentType.Source))
                {
                    Entity entity = new Entity();
                    entity.EntityTypeId = currentType.Id;
                    entity.Name = r[currentType.NameValue].ToString();
                    entity.ParentId = parent.Id;
                    entity.TopLevelId = topLevel.Id;

                    entity.Id = ForwarderCommunication.AddEntity(entity);

                    Logging.LogInformation(string.Format("Parsing results from EntityType: {0} - {1}",
                        currentType.NameValue, entity.Name));

                    foreach (EntityTypeProperty etp in MainService.EntityTypePropertyList.Where(etp => etp.EntityTypeId == currentType.Id))
                    {
                        EntityProperty entityProperty = new EntityProperty();
                        entityProperty.Entity = entity;
                        entityProperty.EntityTypeProperty = etp;
                        entityProperty.BaseType = etp.TypeValue.ToString();
                        entityProperty.StringValue = r[etp.Name].ToString();
                        switch (etp.TypeValue)
                        {
                            case 1:
                                entityProperty.IntValue = Convert.ToInt32(r[etp.Name]);
                                break;
                        }
                        entityProperty.Id = ForwarderCommunication.AddEntityProperty(entityProperty);
                        Helper.CheckAlertRule(entityProperty);
                        Logging.LogInformation(string.Format("Property {0} has a value of {1} of type {2}",
                            etp.Name, entityProperty.StringValue, etp.TypeValue.ToString()));
                    }
                    foreach (EntityType et in MainService.EntityTypeList.Where(et => et.Parent == currentType.Uid))
                    {
                        ProcessSqlCheck(connectionString, et, entity, topLevel);
                    }
                }
            }            
        }
    }
}
